<template>
  <el-aside :width="isCollapse? '64px':'180px'">
    <el-menu
      :collapse="isCollapse"
      :collapse-transition="true"
      :router="true"
      :default-active="route.path"
    >
      <h3 v-show="!isCollapse">后台管理系统</h3>
      <h3 v-show="isCollapse">后台</h3>
      <el-menu-item v-for="item in noChildren"
                    :key="item.path"
                    :index="item.path"
                    @click="handleMenu(item)">
        <component class="icons" :is="item.meta.icon"></component>
        <span>{{ item.meta.title }}</span>
      </el-menu-item>
      <el-sub-menu v-for="item in hasChildren" :key="item.path" :index="item.path">
        <template #title>
          <component class="icons" :is="item.meta.icon"></component>
          <span>{{ item.meta.title }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item v-for="(subItem, subIndex) in item.children"
                        :key="subItem.path"
                        :index="subItem.path"
                        @click="handleMenu(item)">
            <component class="icons" :is="subItem.meta.icon"></component>
            <span>{{ subItem.meta.title }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>

    </el-menu>
  </el-aside>
</template>
<script setup>

import { useSettingStore } from '@/store';
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useUserInfoStore } from '@/store/userInfo.js'
const userInfoStore = useUserInfoStore();
const menuList = computed(() => userInfoStore.menuList)
const noChildren = computed(() => menuList.value.filter(item => !item.children))
const hasChildren = computed(() => menuList.value.filter(item => item.children))

const settingStore = useSettingStore();
const isCollapse = computed(()=>settingStore.isCollapse)
const route = useRoute()
const handleMenu = (menu) => {
  settingStore.selectMenu(menu)
}
</script>

<style scoped>
.icons {
  width: 18px;
  margin-right: 10px;
}

.el-menu {
  height: 100vh;
  border-right: none;

  h3 {
    line-height: 60px;
    font-size: 18px;
    text-align: center;
  }
  .el-menu-item{

    &.is-active{
      color: #fff;
      background-color: #8f9cff;
    }
  }


}
</style>
